
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
errorhandler
Advanced tools
The errorhandler npm package is an error handler middleware for use with Express.js applications. It provides full stack traces and error message responses for errors during development, and a minimal response not leaking internal information in production.
Development Error Handling
In a development environment, errorhandler can provide detailed stack traces and error messages to the console and the client, helping developers debug issues.
app.use(errorhandler({ log: errorNotification }))
function errorNotification(err, str, req) {
var title = 'Error in ' + req.method + ' ' + req.url;
console.error(title, str);
}
Production Error Handling
In a production environment, errorhandler can be used to send a generic error message to the client, without leaking sensitive stack trace information.
if (process.env.NODE_ENV === 'production') {
app.use(errorhandler());
}
This package is a drop-in replacement for Express's default error handler. It automatically catches errors from async routes and passes them to your error handlers. It does not provide the same level of detail for stack traces as errorhandler, but it is useful for handling errors in asynchronous code.
This is an error handler for use with Express.js applications, similar to errorhandler. It is designed to provide a more secure, production-ready error handling solution. It can be configured to hide or show stack traces and error details based on the environment.
Development-only error handler middleware.
This middleware is only intended to be used in a development environment, as the full error stack traces and internal details of any object passed to this module will be sent back to the client when an error occurs.
When an object is provided to Express as an error, this module will display as much about this object as possible, and will do so by using content negotiation for the response between HTML, JSON, and plain text.
Error
object, the string provided by the
stack
property will be returned in HTML/text responses.Error
object, the result of
util.inspect
will be returned in HTML/text responses.This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install errorhandler
var errorhandler = require('errorhandler')
Create new middleware to handle errors and respond with content negotiation.
Error handler accepts these properties in the options object.
Provide a function to be called with the error and a string representation of
the error. Can be used to write the error to any desired location, or set to
false
to only send the error back in the response. Called as
log(err, str, req, res)
where err
is the Error
object, str
is a string
representation of the error, req
is the request object and res
is the
response object (note, this function is invoked after the response has been
written).
The default value for this option is true
unless process.env.NODE_ENV === 'test'
.
Possible values:
true
: Log errors using console.error(str)
.false
: Only send the error back in the response.Basic example of adding this middleware as the error handler only in development
with connect
(express
also can be used in this example).
var connect = require('connect')
var errorhandler = require('errorhandler')
var app = connect()
if (process.env.NODE_ENV === 'development') {
// only use in development
app.use(errorhandler())
}
Sometimes you may want to output the errors to a different location than STDERR during development, like a system notification, for example.
var connect = require('connect')
var errorhandler = require('errorhandler')
var notifier = require('node-notifier')
var app = connect()
if (process.env.NODE_ENV === 'development') {
// only use in development
app.use(errorhandler({ log: errorNotification }))
}
function errorNotification (err, str, req) {
var title = 'Error in ' + req.method + ' ' + req.url
notifier.notify({
title: title,
message: str
})
}
FAQs
Development-only error handler middleware
The npm package errorhandler receives a total of 1,709,405 weekly downloads. As such, errorhandler popularity was classified as popular.
We found that errorhandler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.